package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.InspectionStyle;
import com.hgxd.entity.vo.TimeVo;
import com.hgxd.result.Result;
import com.hgxd.service.InspectionStyleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 巡检方式计划表 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "巡检方式计划")
@RestController
@RequestMapping("/api/inspectionStyle")
@CrossOrigin
public class InspectionStyleController {
    @Resource(name = "InspectionStyleServiceImpl")
    private InspectionStyleService inspectionStyleService;



    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增巡检方式计划", notes = "传入巡检方式计划对象")
    @OperationLogAnnotation(operModule = "巡检方式计划管理-新增", operType = "新增", operContent = "新增巡检方式计划")
    public Result<Boolean> insert(@RequestBody InspectionStyle inspectionStyle) {
        return Result.ok(inspectionStyleService.save(inspectionStyle));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改巡检方式计划", notes = "传入修改的巡检方式计划对象，需传入id")
    @OperationLogAnnotation(operModule = "巡检方式计划管理-修改", operType = "修改", operContent = "修改某巡检方式计划")
    public Result<Boolean> update(@RequestBody InspectionStyle inspectionStyle) {
        return Result.ok(inspectionStyleService.updateById(inspectionStyle));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除", notes = "传入id")
    @OperationLogAnnotation(operModule = "巡检方式计划管理-根据id删除", operType = "删除", operContent = "删除某巡检方式计划")
    public Result<Boolean> delete(@PathVariable("id") Integer id) {
        return Result.ok(inspectionStyleService.removeById(id));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除", notes = "根据id批量删除")
    @OperationLogAnnotation(operModule = "巡检方式计划管理-批量删除", operType = "删除", operContent = "批量删除巡检方式计划")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(inspectionStyleService.removeByIds(ids));
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部", notes = "无需传参")
    @OperationLogAnnotation(operModule = "巡检方式计划管理-查询", operType = "查询", operContent = "查询所有巡检方式计划")
    public Result<List<InspectionStyle>> findAll() {
        return Result.ok(inspectionStyleService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @OperationLogAnnotation(operModule = "巡检方式计划管理-查询", operType = "查询", operContent = "查询某个巡检方式计划")
    public Result<InspectionStyle> findOne(@PathVariable("id") Integer id) {
        return Result.ok(inspectionStyleService.getById(id));
    }

    /**
     * 分页模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "需要传页码和大小")
    @OperationLogAnnotation(operModule = "巡检方式计划管理-分页模糊查询", operType = "查询", operContent = "分页查询巡检方式计划")
    public Result<Page<InspectionStyle>> findPage(@RequestParam Integer pageNum,
                                                       @RequestParam Integer pageSize,
                                                       @RequestBody TimeVo timeVo) {
        LambdaQueryWrapper<InspectionStyle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(timeVo!=null,InspectionStyle::getCreateTime, timeVo.getBeginTime(), timeVo.getEndingTime());
        // 倒序查询
        queryWrapper.orderByDesc(InspectionStyle::getId);
        return Result.ok(inspectionStyleService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


    /**
     * 根据InspectionId查询对应的计划方式（可能有多个）
     * @param inspectionId
     * @return
     */
    @GetMapping("/getInspectStyleBy/{InspectionId}")
    @ApiOperation(value = "根据InspectionId查询计划方式", notes = "可能有多个需用List集合")
    @OperationLogAnnotation(operModule = "巡检方式计划管理-根据InspectionId查询", operType = "查询", operContent = "根据InspectionId查询对应的计划方式（可能有多个）")
    public Result<List<InspectionStyle>> getInspectStyleByInspectionId(@PathVariable("InspectionId") Integer inspectionId){
        List<InspectionStyle> list = inspectionStyleService.getInspectStyleByInspectionId(inspectionId);
        return Result.ok(list);
    }
}

